package com.zh.api.controller.goods;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.utils.PageUtils;
import com.zh.common.utils.Query;
import com.zh.common.utils.ResultVO;
import com.zh.goods.domain.GoodsSublistDO;
import com.zh.goods.service.GoodsSublistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 商品子表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-03-04 16:00:59
 */
 
@Controller
@RequestMapping("/goods/scGoodsSublist")
@Api("子商品管理")
public class GoodsSublistController {
	@Autowired
	private GoodsSublistService scGoodsSublistService;

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取子商品列表")
	public PageUtils list(@RequestBody Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GoodsSublistDO> scGoodsSublistList = scGoodsSublistService.list(query);
		int total = scGoodsSublistService.count(query);
		PageUtils pageUtils = new PageUtils(scGoodsSublistList, total);
		return pageUtils;
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("保存子商品")
	public ResultVO save(@RequestBody GoodsSublistDO scGoodsSublist){
		if(scGoodsSublistService.save(scGoodsSublist)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("修改子商品")
	public ResultVO update(@RequestBody GoodsSublistDO scGoodsSublist){
		 if(scGoodsSublistService.update(scGoodsSublist)>0){
			 return ResultVO.success();
		 }
		return ResultVO.error();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("删除子商品")
	public ResultVO remove(@RequestBody GoodsSublistDO scGoodsSublist){
		if(scGoodsSublistService.remove(scGoodsSublist.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除子商品")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		String[] ids= (String[]) jsonParam.get("ids");
		if(scGoodsSublistService.batchRemove(ids)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	/*
	*
	* 上架1/下架0
	* */
	@PostMapping("/shelf")
	@ResponseBody
	@ApiOperation("子商品上/下架")
	ResultVO  shelf(String id, int type){
		GoodsSublistDO scGoodsSublist=scGoodsSublistService.get(id);
		scGoodsSublist.setFlag(type);
		if(scGoodsSublistService.update(scGoodsSublist)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}


	@ResponseBody
	@GetMapping("/listAll")
	@ApiOperation("获取子商品详情列表")
	public PageUtils listAll(@RequestParam Map<String, Object> params, HttpServletRequest request){
		//查询列表数据

		String id = request.getParameter("id");
		Query query = new Query(params);
		List<GoodsSublistDO> scGoodsSublistList = scGoodsSublistService.listAll(id);
		int total = scGoodsSublistService.countAdd(id);
		PageUtils pageUtils = new PageUtils(scGoodsSublistList, total);
		return pageUtils;
	}

}
